package User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import User.LibraryGUI;
import User.LibraryApp;
import User.Continueborrow;


public class LibraryManagementSystemHomePage extends JFrame {

    private JPanel leftPanel;
    String username;
    private JPanel rightPanel;

    public LibraryManagementSystemHomePage(String username) {
    this.username = username;
        setTitle("图书管理系统");
        setSize(1000, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        leftPanel = new JPanel();
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
        leftPanel.setPreferredSize(new Dimension(200, getHeight()));
        JButton btnBookSearch = new JButton("图书借阅");
        JButton btnBookBorrow = new JButton("图书归还");
        JButton btnBookReturn = new JButton("图书续借");
        JButton btnUserManage = new JButton("用户管理");
        JPanel libraryGUIPanel = getLibraryGUIPanel();
        JPanel libraryAppPanel = getLibraryAppPanel();
        JPanel continueborrowPanel = getContinueborrowPanel();
        JPanel passwordPanel = getPasswordPanel();
        btnBookSearch.addActionListener(new ButtonClickListener(libraryGUIPanel));
        btnBookBorrow.addActionListener(new ButtonClickListener(libraryAppPanel));
        btnBookReturn.addActionListener(new ButtonClickListener(continueborrowPanel));
        btnUserManage.addActionListener(new ButtonClickListener(passwordPanel));
        leftPanel.add(btnBookSearch);
        leftPanel.add(btnBookBorrow);
        leftPanel.add(btnBookReturn);
        leftPanel.add(btnUserManage);
        rightPanel = new JPanel();
        rightPanel.setLayout(new FlowLayout());
        add(leftPanel, BorderLayout.WEST);
        add(rightPanel, BorderLayout.CENTER);
        setVisible(true);
    }

    private class ButtonClickListener implements ActionListener {
        private JPanel contentPanel;

        public ButtonClickListener(JPanel contentPanel) {
            this.contentPanel = contentPanel;
        }

        @Override
        public void actionPerformed(ActionEvent e) {


            rightPanel.removeAll();
            rightPanel.add(contentPanel);
            rightPanel.revalidate();
            rightPanel.repaint();
    }
    }


    private JPanel getLibraryGUIPanel() {
        LibraryGUI libraryGUI = new LibraryGUI(username);

        libraryGUI.setVisible(false);
        return libraryGUI.getContentPanel();
    }

    private JPanel getLibraryAppPanel() {
        LibraryApp libraryApp = new LibraryApp(username);
        libraryApp.setVisible(false);
        return libraryApp.getContentPanel();
    }

    private JPanel getContinueborrowPanel() {
        Continueborrow continueborrow = new Continueborrow(username);
        continueborrow.setVisible(false);

        return continueborrow.getContentPanel();
    }
    private JPanel getPasswordPanel() {
        Password password = new Password(username);
        password.setVisible(false);
        return password.getContentPanel();
    }

    public static void main(String[] args) {
        new LibraryManagementSystemHomePage("张彻6666");
    }

}